<template>
  <div class="app-container" v-cloak>
    <el-row :gutter="20">
      <!--      &lt;!&ndash;部门数据&ndash;&gt;
            <el-col :span="4" :xs="24">
              <div class="head-container">
                <el-input
                  v-model="deptName"
                  placeholder="请输入部门名称"
                  clearable
                  size="small"
                  prefix-icon="el-icon-search"
                  style="margin-bottom: 20px"
                />
              </div>
              <div class="head-container">
                <el-tree
                  :data="deptOptions"
                  :props="defaultProps"
                  :expand-on-click-node="false"
                  :filter-node-method="filterNode"
                  ref="tree"
                  default-expand-all
                  @node-click="handleNodeClick"
                />
              </div>
            </el-col>-->
      <table>
        <tr>
          <td><span>消息标题：</span></td>
          <td><el-input v-model="form.title" placeholder="请输入文档标题" style="width: 240px"></el-input></td>

        </tr>
        <br>
        <tr>
          <td><span>推送类型：</span></td>
          <td  style="position: absolute;z-index: 1000;text-align: center">

            <el-select v-model="form.msg_type_id" placeholder="请选择">
                <el-option
                  v-for="dict in status"
                  :key="dict.dict_code"
                  :label="dict.dict_label"
                  :value="dict.dict_code"
                />
              </el-select>
          </td>
        </tr>
        <br>
        <br>
        <tr>
          <td><span>消 息 外 链:</span></td>
          <td><el-input v-model="form.url" placeholder="请输入文档外链地址" style="width: 240px"></el-input></td>
        </tr>
        <br>
        <br>
        <tr>
          <td><span>消息内容：</span></td>
          <td>
            <editor style="position: relative;z-index: 0;" ref="editorOne" v-model="form.content"  @change="change"></editor>
          </td>

        </tr>
        <br>


        <tr>
          <td></td>
          <td>
            <el-button type="primary" @click="submit">提交</el-button>
            <el-button type="info" @click="reset" style="margin-left: 30px;">重置</el-button></td>
        </tr>
      </table>

    </el-row>
  </div>
</template>

<script>

  import { add } from "@/api/system/user";

  import { getToken } from "@/utils/auth";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  import Editor from '@/components/wangEditor'

  export default {
    name: "File",
    components: { Treeselect  ,Editor },
    data() {
      return {
        isClear: false,

        detail:"",
        user: {},
        sexs: [],
        option:[],
        option1:[],
        id_card_type: [],
        status:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 默认密码
        initPassword: undefined,
        // 日期范围
        dateRange: [],
        // 状态数据字典
        statusOptions: [],
        // 性别状态字典
        Options: [],
        // 岗位选项
        postOptions: [],
        // 角色选项
        roleOptions: [],
        // 表单参数
        form: {},
        defaultProps: {
          children: "children",
          label: "label"
        },
        // 用户导入参数
        upload: {
          url: process.env.VUE_APP_BASE_API + "/uploadFileImg",

        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 1000,
          name: undefined,
          status: undefined,
          parent_id:'0'
        },
        /*        // 表单校验
                rules: {
                  userName: [
                    { required: true, message: "用户名称不能为空", trigger: "blur" }
                  ],
                  nickName: [
                    { required: true, message: "用户昵称不能为空", trigger: "blur" }
                  ],

                  email: [
                    {
                      type: "email",
                      message: "'请输入正确的邮箱地址",
                      trigger: ["blur", "change"]
                    }
                  ],
                  phonenumber: [
                    {
                      pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                      message: "请输入正确的手机号码",
                      trigger: "blur"
                    }
                  ]
                }*/
        // 查询参数
        queryParams1: {
          pageNum: 1,
          pageSize: 1000,
        },
        // 日期范围
        dateRange1: [],
        templateOption:[],
        queryParams3: {
          pageNum: 1,
          pageSize: 1000,
        },
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false
      };
    },
    created() {
      this.getDicts("sys_subscription_type").then(response => {
        this.status = response.data.items;
      });
    },

    methods: {
      change(val) {
        console.log(val)

      },
      submit () {
        console.log(this.form)
        add(this.form).then(rs =>{
          this.msgSuccess("推送消息成功!");
        })
      },
      // 表单重置
      reset() {
        this.form = {
          file_title: undefined,
          file_content: undefined,
          template_id: undefined,
          channel_id: undefined,
        };
        this.resetForm("form");
      },
      //获取频道
      getListChannle(){
        listChannel(this.addDateRange(this.queryParams, this.dateRange)).then(rs =>{
          this.option = rs.data.items;
        })
      },
      /*文档附件上传*/
      handleAccessoryChangeIcon(file, fileList) {//选中文件触发的change事件
        console.log(file)
        const isJPG = file.raw.type === 'image/jpeg'
        const isPNG = file.raw.type === 'image/png'
        // const isLt2M = file.raw.size / 1024 / 1024 < 0.5  //限制上传文件的大小
        if (!isPNG && !isJPG) {
          this.$message.error('上传图片只能是 JPG/PNG 格式!')
          return false
        } else {
          this.form.accessory = URL.createObjectURL(file.raw);//赋值图片的url，用于图片回显功能
          this.uploadImg(file)//调用上传文件api接口
          this.$forceUpdate()
        }
      },
      uploadImg(file){//文件上传操作
        let formData=new FormData()
        formData.append('file',file.raw)
        uploadFileImg(formData).then(res=>{
          console.log(res)
          if (res.code == '20000') {
            this.msgSuccess(res.data.msg);
            this.form.accessory = res.data[0].url;
            console.log(this.form.accessory)
          }
        })
      },
      //删除列表中的图片
      handleRemove(file) {

        var url = file.response.data[0].url;
        var accessory = this.form.accessory
        let strings = accessory.split(",");
        for (let i = 0; i < strings.length; i++) {
          if (strings[i] == url) {
            strings.splice(i,1);
          }
        }
        var rs = strings.join();
        this.form.accessory = rs;
        console.log(this.form.accessory )
        this.$refs.pictureUpload.handleRemove(file)
      },
      //预览图片
      handlePictureCardPreview(file) {
        console.log(file)
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      //下载图片
      handleDownload(file) {
        console.log(file);
      },
      //上传成功回调函数
      handleSuccess(res) {

        var accessory = this.form.accessory;
        if (accessory == undefined || accessory.length==0){
          this.form.accessory= res.data[0].url;
        } else {
          this.form.accessory = this.form.accessory+','+res.data[0].url;
        }
        console.log(this.form.accessory)
        /*this.$set(this.model, 'icon', res)*/
      },
    }

  };
</script>
<style>
  [v-cloak]{
    display: none !important;
  }
</style>
